use crate::webservice::cas::auth;
use actix_web::{get, http::Cookie, HttpRequest, HttpResponse, Responder,HttpMessage};
use qstring::QString;
#[get("/sso")]
pub async fn sso(req: HttpRequest) -> impl Responder {
    //获取ticket
    let query_str = req.query_string();
    let qs = QString::from(query_str);
    if let Some(ticket) = qs.get("ticket") {
        //获取验证信息
        let validate_response = auth::get_logined_info_from_redis_by_ticket(&ticket)
            .await
            .unwrap();

        HttpResponse::Ok()
            .content_type("text/plain;charset=utf-8")
            .cookie(
                Cookie::build(auth::TICKET_COOKIE_NAME.to_owned(), ticket.to_owned())
                    .path("/")
                    .secure(false)
                    .http_only(false)
                    .finish(),
            )
            .body(validate_response.to_owned())
    } else {
        if let Some(cookie) = req.cookie(auth::TICKET_COOKIE_NAME) {
            //先看cookie有没有ticket
            let ticket = cookie.value();
            //获取验证信息
            let validate_response = auth::get_logined_info_from_redis_by_ticket(&ticket)
                .await
                .unwrap();
            HttpResponse::Ok()
                .content_type("text/plain;charset=utf-8")
                .cookie(
                    Cookie::build(auth::TICKET_COOKIE_NAME.to_owned(), ticket.to_owned())
                        .path("/")
                        .secure(false)
                        .http_only(false)
                        .finish(),
                )
                .body(validate_response.to_owned())
        } else {
            HttpResponse::Ok()
                .content_type("application/json;charset=utf-8")
                .body("{code:0,msg:'ok'}")
        }
    }
}
